import java.util.Scanner;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // 创建 ClassManager 的实例
        ClassManager classManager = new ClassManager();
        // 创建 StudentManager 的实例
        StudentManager studentManager = new StudentManager();
        // 创建 RandomCaller 的实例
        RandomCaller randomCaller = new RandomCaller();
        // 创建 ScoreRecorder 的实例
        ScoreRecorder scoreRecorder = new ScoreRecorder();

        // 创建 Scanner 对象用于读取用户输入
        Scanner scanner = new Scanner(System.in);

        boolean running = true;
        while (running) {
            // 显示菜单选项
            System.out.println("Menu:");
            System.out.println("1. Add Class");
            System.out.println("2. Add Group to Class");
            System.out.println("3. Add Student to Group");
            System.out.println("4. Random Call");
            System.out.println("5. Show Scores and Attendance");
            System.out.println("6. Add Score to Student");
            System.out.println("7. Add Score to Group");
            System.out.println("8. Record Student Attendance");
            System.out.println("9. Exit");

            System.out.print("Choose option: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗掉换行符

            switch (choice) {
                case 1:
                    // 添加班级
                    System.out.print("Enter class name: ");
                    String className = scanner.nextLine();
                    classManager.addClass(className); // 调用 addClass 方法
                    break;
                case 2:
                    // 添加组到班级
                    List<String> availableClasses = classManager.getClassList();
                    // 检查是否有可用的班级
                    if (!availableClasses.isEmpty()) {
                        for (String c : availableClasses) {
                            System.out.println("- " + c);
                        }
                    } else {
                        System.out.println("No classes available.");
                    }
                    System.out.print("Enter class name: ");
                    String selectedClassName = scanner.nextLine();
                    System.out.print("Enter group name: ");
                    String groupName = scanner.nextLine();
                    classManager.getClassGroups(selectedClassName).addGroup(groupName); // 添加组
                    break;
                case 3:
                    // 添加学生到组
                    availableClasses = classManager.getClassList();
                    if (!availableClasses.isEmpty()) {
                        for (String c : availableClasses) {
                            System.out.println("- " + c);
                        }
                    } else {
                        System.out.println("No classes available.");
                    }
                    System.out.print("Enter class name: ");
                    String selectedClass = scanner.nextLine();
                    List<String> availableGroups = classManager.getClassGroups(selectedClass).getGroupList();
                    if (!availableGroups.isEmpty()) {
                        for (String g : availableGroups) {
                            System.out.println("- " + g);
                        }
                    } else {
                        System.out.println("No groups available.");
                    }
                    System.out.print("Enter group name: ");
                    String selectedGroup = scanner.nextLine();
                    System.out.print("Enter student name: ");
                    String studentName = scanner.nextLine();
                    classManager.getClassGroups(selectedClass).addStudentToGroup(selectedGroup, studentName); // 添加学生到组
                    studentManager.addStudentToGroup(selectedGroup, studentName); // 在 StudentManager 中添加学生
                    studentManager.addStudentToClass(selectedClass, studentName); // 在 StudentManager 中添加学生到班级
                    break;
                case 4:
                    // 随机点名
                    System.out.println("Choose call type:");
                    System.out.println("a. From Class");
                    System.out.println("b. From Group");
                    System.out.print("Enter choice: ");
                    char callType = scanner.next().charAt(0);
                    scanner.nextLine(); // 消耗掉换行符

                    if (callType == 'a') {
                        availableClasses = classManager.getClassList();
                        if (!availableClasses.isEmpty()) {
                            for (String c : availableClasses) {
                                System.out.println("- " + c);
                            }
                        } else {
                            System.out.println("No classes available.");
                        }
                        System.out.print("Enter class name: ");
                        String selectedClassForCall = scanner.nextLine();
                        List<String> classStudents = studentManager.getStudentsInClass(selectedClassForCall);
                        String randomStudentInClass = randomCaller.getRandomStudentFromClass(classStudents);
                        System.out.println("Random Student in Class: " + randomStudentInClass);
                    } else if (callType == 'b') {
                        availableClasses = classManager.getClassList();
                        if (!availableClasses.isEmpty()) {
                            for (String c : availableClasses) {
                                System.out.println("- " + c);
                            }
                        } else {
                            System.out.println("No classes available.");
                        }
                        System.out.print("Enter class name: ");
                        String selectedClassForGroup = scanner.nextLine();
                        availableGroups = classManager.getClassGroups(selectedClassForGroup).getGroupList();
                        if (!availableGroups.isEmpty()) {
                            for (String g : availableGroups) {
                                System.out.println("- " + g);
                            }
                        } else {
                            System.out.println("No groups available.");
                        }
                        System.out.print("Enter group name: ");
                        String selectedGroupForCall = scanner.nextLine();
                        List<String> groupStudents = classManager.getClassGroups(selectedClassForGroup).getStudentsInGroup(selectedGroupForCall);
                        String randomStudentInGroup = randomCaller.getRandomStudentFromGroup(groupStudents);
                        System.out.println("Random Student in Group: " + randomStudentInGroup);
                    }
                    break;
                case 5:
                    // 显示分数和出勤记录
                    System.out.println("Student Scores: " + scoreRecorder.getStudentScores());
                    System.out.println("Group Scores: " + scoreRecorder.getGroupScores());
                    System.out.println("Student Attendance: " + scoreRecorder.getStudentAttendance());
                    break;
                case 6:
                    // 添加学生的分数
                    System.out.print("Enter student name: ");
                    String studentToAddScore = scanner.nextLine();
                    System.out.print("Enter score: ");
                    int studentScore = scanner.nextInt();
                    scanner.nextLine(); // 消耗掉换行符
                    scoreRecorder.addStudentScore(studentToAddScore, studentScore);
                    break;
                case 7:
                    // 添加小组的分数
                    System.out.print("Enter group name: ");
                    String groupToAddScore = scanner.nextLine();
                    System.out.print("Enter score: ");
                    int groupScore = scanner.nextInt();
                    scanner.nextLine(); // 消耗掉换行符
                    scoreRecorder.addGroupScore(groupToAddScore, groupScore);
                    break;
                case 8:
                    // 记录学生的考勤
                    System.out.print("Enter student name: ");
                    String studentToRecordAttendance = scanner.nextLine();
                    System.out.println("Is the student present? (y/n)");
                    String attendanceInput = scanner.nextLine();
                    boolean attendance = attendanceInput.equalsIgnoreCase("y");
                    scoreRecorder.recordStudentAttendance(studentToRecordAttendance, attendance);
                    break;
                case 9:
                    // 退出程序
                    running = false;
                    break;
                default:
                    System.out.println("Invalid choice!");
            }
        }

        // 关闭 Scanner 对象
        scanner.close();
    }
}
